//
//  InviteFriendsViewController.m
//  Article
//
//  Created by Haricot_cpc17 on 16/4/22.
//  Copyright © 2016年 mac. All rights reserved.
//
//邀请好友
#import "InviteFriendsViewController.h"

@interface InviteFriendsViewController ()

@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UIImage *image;

@end

@implementation InviteFriendsViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];
    
    [self createQRCode];
    // Do any additional setup after loading the view.
}
-(void)createUI{
    
    self.view.backgroundColor = UIColorFromRGB(MYBlue);
    
    //二维码
    _imageView =[[UIImageView alloc]initWithFrame:CGRectMake((WIDTH-WIDTH/3)/4, self.view.bounds.size.height*3/10, WIDTH-WIDTH/3, WIDTH-WIDTH/3)];
    _imageView.backgroundColor =[UIColor blackColor];
    [self.view addSubview:_imageView];
}
#pragma mark 原生二维码生成
- (void)createQRCode {
    
    // 创建过滤器
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 恢复默认
    [filter setDefaults];
    
    // 给过滤器添加数据
    NSString *dataString = @"鲁能是冠军！！";
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKey:@"inputMessage"];
    
    // 获取输出的二维码
    CIImage *outputImage = [filter outputImage];
    
    
    // 获取高清的二维码 200 * 200
    _image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200.0];
    _imageView.image = _image;
    NSLog(@"%@",dataString);
    // 添加logo
    CALayer *logoLayer = [CALayer layer];
    
    logoLayer.frame = CGRectMake(105, 100, 50, 50);
    
    logoLayer.contents = (__bridge id)[UIImage imageNamed:@"头像"].CGImage;
    
    [_imageView.layer addSublayer:logoLayer];
    
}
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size {
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size / CGRectGetWidth(extent), size / CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
